"use strict";

const { TaiSpider } = require("tai-spider");
const { JSDOM } = require("jsdom");

class SXGovSpider extends TaiSpider {
  constructor(options = {}) {
    super(options);
    this.name = "sx-gov";
    this.debug = true;
    this.start_urls = [
      "http://www.sx.gov.cn/col/col1229439406/index.html?uid=6459046&pageNum=1",
    ];
  }

  *parse(response) {
    // 将这段字符串转换成一个 DOM 对象
    let contentString = response.css(".Con-right_list div").get(0).extract();
    const dom = new JSDOM(contentString);
    // 在模拟的浏览器窗口对象中设置全局的 `window` 和 `document` 对象
    const aDoms = dom.window.document.querySelectorAll("a");
    for (let elem of aDoms) {
      yield response.follow(elem.href, this.parseDetail);
    }
  }

  *parseDetail(response) {
    yield {
      title: response.css("table").get(0).css("table tr td").get(0).extract(),
      content: response.css("table").get(1).css("table tr td").get(0).extract(),
      partment: response
        .css("table")
        .get(2)
        .css("table tr td")
        .get(1)
        .extract(),
      start: response.css("table").get(2).css("table tr td").get(2).extract(),
      end: response.css("table").get(2).css("table tr td").get(3).extract(),
    };
  }
}

module.exports = SXGovSpider;
